package com.prs.crm.action.exchange;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.courser.Util;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import org.hibernate.criterion.DetachedCriteria;

import com.prs.crm.context.AppContext;
import com.prs.crm.domain.exchange.OnlinePerson;
import com.prs.crm.service.exchange.OnlinePersonService;

public class OnlinePersonAction extends BaseAction  implements ServletRequestAware{

	private static final long serialVersionUID = 1L;
	
	private OnlinePersonService onlinePersonService;
	
	private HttpServletRequest servletRequest;
	
	private PaginationSupport pagination;
	
	private OnlinePerson onlinePerson;
	
	private Integer id;
	
	private Long onlineMinutes;

	public void setOnlinePersonService(OnlinePersonService onlinePersonService) {
		this.onlinePersonService = onlinePersonService;
	}

	public OnlinePersonService getOnlinePersonService() {
		return onlinePersonService;
	}
	
    public void setOnlinePerson(OnlinePerson onlinePerson) {
		this.onlinePerson = onlinePerson;
	}

	public OnlinePerson getOnlinePerson() {
		return onlinePerson;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public Integer getId() {
		return id;
	}

	public Long getOnlineMinutes() {
		return onlineMinutes;
	}

	public void setOnlineMinutes(Long onlineMinutes) {
		this.onlineMinutes = onlineMinutes;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	public PaginationSupport getPagination() {
		return pagination;
	}

	public HttpServletRequest getServletRequest() {
        return servletRequest;
    }

    public void setServletRequest(HttpServletRequest servletRequest) {
        this.servletRequest = servletRequest;
    }

	
	@Action(value="ping",results={@Result(name="success",type="json",params={"root","prompt"})})
	public String ping()
	{
		if (AppContext.getAccountContext()!=null)
		{
			this.getOnlinePersonService().savePing(AppContext.getAccountContext().currentUser()
				,Util.getRemoteIp(servletRequest));
		}
		return SUCCESS;
	}
	@Action(value="countOnlinePersons",results={@Result(name="success",type="json",params={"root","prompt"})})
	public String countOnlinePersons(){
		this.setPrompt(String.valueOf(this.getOnlinePersonService().getAll().size()));
		return SUCCESS;
	}
	
	@Action("listOnline")
	public String listOnline()
	{
		DetachedCriteria criteria = this.getPaginationHelper().createQuery(OnlinePerson.class);
		this.setPagination(this.getOnlinePersonService().listByPage(criteria, this.getPaginationHelper().prepare(pagination)));
		return SUCCESS;
	}
	@Action("viewOnline")
	public String viewOnline()
	{
		this.setOnlinePerson(this.getOnlinePersonService().get(id));
		this.onlineMinutes = (Util.nowFullTime().getTime()-onlinePerson.getLoginTime().getTime())/(1000*60);
		return SUCCESS;
	}
}
